package com.zhilianbao.leyaogo.ui.fragment.shoppingcart;

import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.bql.pickerview.NormalPickerView;
import com.bql.utils.CheckUtils;
import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.ui.fragment.base.BaseFragment;
import com.zhilianbao.leyaogo.utils.XToastUtils;
import com.zhilianbao.leyaogo.view.widgets.AwesomeEditTextView;
import com.zhilianbao.leyaogo.view.widgets.ModifyView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Author: hcy
 * Description:发票
 * Created: 2016/10/25 14:27
 */
public class InvoiceFragment extends BaseFragment implements ModifyView.OnItemClickListener {

    private static final int TAG_TYPE = 0, TAG_TITLE = 1, TAG_CONTENT = 2;

    @BindView(R.id.et_company)
    public AwesomeEditTextView mCompanyEt;

    @BindView(R.id.mv_invoice_title)
    ModifyView mModifyView;

    @BindView(R.id.switch_invoice)
    SwitchCompat mSwitchCompat;

    @BindView(R.id.deliver)
    View mDeliver;

    private ArrayList<String> mTitleList;
    private NormalPickerView mTitlePV;
    private int type = -1;//抬头类型，0个人，1公司,-1不选
    private String title;

    public static InvoiceFragment getInstance(Invoice invoice) {
        Bundle bundle = new Bundle();
        int type = -1;
        String title = "";
        if(invoice!=null&&invoice.type!=-1){
            type = invoice.type;
            title = invoice.invoiceTitle;
        }
        bundle.putString(IntentConfig.INVOICE_TITLE, title);
        bundle.putInt(IntentConfig.INVOICE_TITLE_TYPE, type);
        InvoiceFragment fragment = new InvoiceFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_invoice;
    }

    @Override
    protected void initToolbarHere() {
        initToolbar(getString(R.string.invoice), "", getString(R.string.invoice_save), 0, 0, 0, 0);
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        //数据
        List<String> tempList = Arrays.asList(getResources().getStringArray(R.array.invoice_title));
        mTitleList = new ArrayList<>(tempList);
        Bundle bundle = getArguments();
        type = bundle.getInt(IntentConfig.INVOICE_TITLE_TYPE);
        title = bundle.getString(IntentConfig.INVOICE_TITLE);

        if (type!=-1) {
            mSwitchCompat.setChecked(true);
            mModifyView.setModifyItemText(mTitleList.get(type));
        }else {
            showHideSelectInvoice(false);
            mCompanyEt.setFocusable(false);
            mCompanyEt.requestFocus();
        }
        mCompanyEt.setText(title);
        mCompanyEt.setClearDrawable(R.drawable.icon_delete);
        mCompanyEt.setDrawableClearVisible(false);

        mCompanyEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()==0) {
                    mCompanyEt.setDrawableClearVisible(false);
                }else {
                    mCompanyEt.setDrawableClearVisible(true);
                }
            }

        });

        initPickerView();
        initModifyView();

        mSwitchCompat.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isChecked) {
                mCompanyEt.setText("");
                mModifyView.setModifyItemText("");
                mCompanyEt.setFocusable(false);
                mCompanyEt.requestFocus();
                type = -1;
            }
            showHideSelectInvoice(isChecked);
        });
    }

    private void showHideSelectInvoice(boolean show) {
        if (show) {
            mDeliver.setVisibility(View.VISIBLE);
            mModifyView.setVisibility(View.VISIBLE);
            mCompanyEt.setVisibility(View.VISIBLE);
        } else {
            mDeliver.setVisibility(View.GONE);
            mModifyView.setVisibility(View.GONE);
            mCompanyEt.setVisibility(View.GONE);
        }
    }

    /***
     * 初始化PickerView
     */
    private void initPickerView() {

        //抬头
        mTitlePV = new NormalPickerView(mActivity);
        mTitlePV.setPicker(mTitleList);
        mTitlePV.setTitle(mActivity.getResources().getString(R.string.invoice_title));
        mTitlePV.setOnOptionsSelectListener(new NormalPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                mModifyView.setModifyItemText(mTitleList.get(options1));
                mCompanyEt.setFocusable(true);
                mCompanyEt.setFocusableInTouchMode(true);
                mCompanyEt.requestFocus();
                type = options1;
            }

            @Override
            public void onCancel(int options1, int option2, int options3) {
            }
        });
        setPickerViewStyle(mTitlePV);

    }

    /**
     * PickerView样式
     *
     * @param pv
     */
    private void setPickerViewStyle(NormalPickerView pv) {
        pv.setCancelable(true);
        pv.setCyclic(false);
        pv.setContentBackGroundColor(getResources().getColor(R.color.white));
        pv.setLineVisible(false, false);
        pv.setTitleTextColorSize(getResources().getColor(R.color.color_333333), 15);
        pv.setBtnCancelTextColorSize(getResources().getColor(R.color.color_333333), 14);
        pv.setBtnSubmitTextColorSize(getResources().getColor(R.color.color_333333), 14);
        pv.setStyle(2f, getResources().getColor(R.color.transparent)
                , getResources().getColor(R.color.color_333333), getResources().getColor(R.color.color_999999), 5, 11);
    }

    /**
     * 初始化控件
     */
    private void initModifyView() {
        mModifyView.setItemName(getString(R.string.invoice_title)).setModifyItemHintText(getString(R.string.invoice_title_hint)).setModifyItemCanEdit(false);
        mModifyView.setOnItemClickListener(this, TAG_TITLE);
    }


    @Override
    public void onItemClick(int tag) {
        switch (tag) {
            case TAG_TITLE:
                mTitlePV.show();
                break;
        }
    }

    @Override
    public void btnRightTextClick() {
        super.btnRightTextClick();

        if (mSwitchCompat.isChecked() && CheckUtils.isEmpty(mCompanyEt.getText().toString().trim())) {
            XToastUtils.showShortToast(R.string.invoice_company_hint);
            return;
        }
        Invoice invoice = new Invoice();
        invoice.type = type;
        invoice.invoiceTitle = mCompanyEt.getText().toString().trim();
        invoice.typeStr = mModifyView.getModifyEditText();
        EventBus.getDefault().post(new EventManager<>(LeEventCode.MSG_CODE_INVOICE, invoice));
        pop();
    }

    public class Invoice {
        public int type = -1;//类型1公司，0个人,-1不需要
        public String invoiceTitle = "";//抬头
        public String typeStr = "";//公司，个人
    }
}
